package com.example.fastshare.adapter;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.fastshare.entities.MessageEntity;

public class DBMesssageAdapter extends BaseAdapter {

	private DatabaseAdapter mDatabaseAdapter;
	private Context context;
	private ArrayList<Bitmap> arrayBitMap;

	public DBMesssageAdapter(Context context) {
		super();
		this.mDatabaseAdapter = new DatabaseAdapter(context);
	}

	public DBMesssageAdapter(Context context, ArrayList<Bitmap> arrayBitMap) {
		super();
		this.context = context;
		this.arrayBitMap = arrayBitMap;
	}

	public MessageEntity getMessageByID(String id) {
		MessageEntity result = null;

		String where = MessageEntity.ID + "='" + id + "'";
		Cursor cur = mDatabaseAdapter.query(MessageEntity.TABLE_NAME, null,
				where, null, null, null, null);

		if (cur != null && cur.getCount() > 0) {
			result = getMessageEntity(cur);
		}
		return result;
	}

	public List<MessageEntity> getMessagesOfUser(String user_ID) {
		// CHECKME
		String whereClause = MessageEntity.USER_ID + "='" + user_ID + "'";
		List<MessageEntity> result = null;
		Cursor cur;
		cur = mDatabaseAdapter.query(MessageEntity.TABLE_NAME, null,
				whereClause, null, null, null, null);
		if (cur != null && cur.getCount() > 0) {
			cur.moveToFirst();
			result = new ArrayList<MessageEntity>();
			while (!cur.isAfterLast()) {
				result.add(getMessageEntity(cur));
				cur.moveToNext();
			}
		}
		cur.close();
		return result;
	}

	public MessageEntity getLastMessagesOfUser(String user_ID) {
		// CHECKME
		String whereClause = MessageEntity.USER_ID + "='" + user_ID + "'";
		MessageEntity result = null;
		Cursor cur;
		cur = mDatabaseAdapter.query(MessageEntity.TABLE_NAME, null,
				whereClause, null, null, null, null);
		if (cur != null && cur.getCount() > 0) {
			cur.moveToLast();
			result = getMessageEntity(cur);
		}
		cur.close();
		return result;
	}

	public List<MessageEntity> getMessagesOfGroup(String group_ID) {
		// CHECKME
		String whereClause = MessageEntity.GROUP_ID + "='" + group_ID + "'";
		List<MessageEntity> result = null;
		Cursor cur;
		cur = mDatabaseAdapter.query(MessageEntity.TABLE_NAME, null,
				whereClause, null, null, null, null);

		if (cur != null && cur.getCount() > 0) {
			cur.moveToFirst();
			result = new ArrayList<MessageEntity>();
			while (!cur.isAfterLast()) {
				result.add(getMessageEntity(cur));
				cur.moveToNext();
			}
		}
		cur.close();
		return result;
	}

	public MessageEntity getLastMessagesOfGroup(String group_ID) {
		// CHECKME
		String whereClause = MessageEntity.GROUP_ID + "='" + group_ID + "'";
		MessageEntity result = null;
		Cursor cur;
		cur = mDatabaseAdapter.query(MessageEntity.TABLE_NAME, null,
				whereClause, null, null, null, null);

		if (cur != null && cur.getCount() > 0) {
			cur.moveToFirst();
			result = getMessageEntity(cur);
		}
		cur.close();
		return result;
	}

	public List<MessageEntity> getAllMessages() {
		List<MessageEntity> result = null;

		String orderASC = MessageEntity.ID + " ASC ";

		Cursor cur = mDatabaseAdapter.query(MessageEntity.TABLE_NAME, null,
				null, null, null, null, orderASC);

		if (cur != null && cur.getCount() > 0) {
			result = new ArrayList<MessageEntity>();
			while (!cur.isAfterLast()) {
				result.add(getMessageEntity(cur));
				cur.moveToNext();
			}
		}
		
		Collections.sort(result);
		cur.close();
		return result;
	}

	public List<MessageEntity> getAllDistinctMessages() {
		List<MessageEntity> result = null;

		String sql = "SELECT DISTINCT(" + MessageEntity.USER_ID + ") FROM "
				+ MessageEntity.TABLE_NAME + " WHERE " + MessageEntity.GROUP_ID
				+ "=" + "'null'";
		Cursor cur = mDatabaseAdapter.rawQuery(sql, null);

		if (cur != null && cur.getCount() > 0) {
			cur.moveToFirst();
			result = new ArrayList<MessageEntity>();
			while (!cur.isAfterLast()) {
				result.add(getMessageEntity(cur));
				cur.moveToNext();
			}
		}

		sql = "SELECT DISTINCT(" + MessageEntity.USER_ID + ") FROM "
				+ MessageEntity.TABLE_NAME + " WHERE " + MessageEntity.GROUP_ID
				+ "!=" + "'null'";
		cur = mDatabaseAdapter.rawQuery(sql, null);

		if (cur != null && cur.getCount() > 0) {
			cur.moveToFirst();
			while (!cur.isAfterLast()) {
				result.add(getMessageEntity(cur));
				cur.moveToNext();
			}
		}
		cur.close();		
		return result;
	}

	public boolean isEmptyMessage() {
		boolean result = true;

		Cursor cur = mDatabaseAdapter.query(MessageEntity.TABLE_NAME, null,
				null, null, null, null, null);

		if (cur != null && cur.getCount() > 0) {
			result = false;
		}
		cur.close();
		return result;
	}

	public long insertMessage(MessageEntity entity) {
		if (entity != null) {
			ContentValues values = new ContentValues();
			values.put(MessageEntity.ID, entity.getID());
			values.put(MessageEntity.MESSAGE_TYPE, entity.getMessage_type());
			values.put(MessageEntity.USER_ID, entity.getUser_ID());
			values.put(MessageEntity.CONTENT, entity.getContent());
			values.put(MessageEntity.GROUP_ID, entity.getGourp_ID());
			values.put(MessageEntity.IMAGE_ID, entity.getImage_ID());
			values.put(MessageEntity.CONTENT, entity.getContent());
			values.put(MessageEntity.TIME, entity.getTimeInString());

			long result = mDatabaseAdapter.insert(MessageEntity.TABLE_NAME,
					null, values);

			return result;
		} else
			return -1;
	}

	public long deleteMessageByID(String id) {
		String where = MessageEntity.ID + "='" + id + "'";
		return mDatabaseAdapter.delete(MessageEntity.TABLE_NAME, where, null);
	}

	public long deleteAllMessages() {
		return mDatabaseAdapter.delete(MessageEntity.TABLE_NAME, null, null);
	}

	private MessageEntity getMessageEntity(Cursor mCursor) {
		if (mCursor != null) {
			MessageEntity values = new MessageEntity();
			values.setID(mCursor.getString(mCursor
					.getColumnIndex(MessageEntity.ID)));
			values.setGourp_ID(mCursor.getString(mCursor
					.getColumnIndex(MessageEntity.GROUP_ID)));
			values.setImage_ID(mCursor.getString(mCursor
					.getColumnIndex(MessageEntity.IMAGE_ID)));
			values.setMessage_type(mCursor.getString(mCursor
					.getColumnIndex(MessageEntity.MESSAGE_TYPE)));
			values.setUser_ID(mCursor.getString(mCursor
					.getColumnIndex(MessageEntity.USER_ID)));
			values.setContent(mCursor.getString(mCursor
					.getColumnIndex(MessageEntity.CONTENT)));
			values.setTime(mCursor.getString(mCursor
					.getColumnIndex(MessageEntity.TIME)));

			return values;
		}
		return null;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public ArrayList<Bitmap> getArrayBitMap() {
		return arrayBitMap;
	}

	public void setArrayBitMap(ArrayList<Bitmap> arrayBitMap) {
		this.arrayBitMap = arrayBitMap;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return null;
	}
}
